﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Twitterizer.Framework;

namespace TwitterTray
{
    public partial class Login : Form
    {

        

        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool Continue = true;
            
            if (txtUser.Text.Length <= 0)
            {
                MessageBox.Show("Please provide a valid Twitter.com username.", "TwitteryTray! - Missing Information");
                //return null;
                Continue = false;

            }
            if (txtPassword.Text.Length <= 0)
            {
                MessageBox.Show("Please provide a valid password.", "TwitteryTray! - Missing Information");
                //return null;
                Continue = false;
            }
            if (comboBox1.SelectedIndex == -1)
            {

                MessageBox.Show("Please select an update frequency.", "TwitteryTray! - Missing Information");
                Continue = false;

            }

            if (Continue)
            {
                Properties.Settings.Default.password = txtPassword.Text;
                Properties.Settings.Default.username = txtUser.Text;
                char[] splitter = new char[] { ' ' };
                int index = comboBox1.SelectedIndex;
                string text = comboBox1.Items[index].ToString();

                Properties.Settings.Default.updatefreq = int.Parse(text.Split(splitter, 2)[0].Trim());
                Properties.Settings.Default.savesettings = checkBox1.Checked;
                Properties.Settings.Default.Save();
                
                Twitter Twit = new Twitter(Properties.Settings.Default.username, Properties.Settings.Default.password);
                
                try
                {
                    this.Enabled = false;
                    this.Cursor = Cursors.WaitCursor;
                    this.Update();

                    LBLloginStatus.Text = "Authenticating to Twitter.com";

                    TwitterStatusCollection Stats = Twit.FriendsTimeline();

                    this.Update();

                    LBLloginStatus.Text = "Authenticated!";
                    this.Hide();
                    this.TopLevel = false;
                    UpdateStatus StatusForm = new UpdateStatus();
                    StatusForm.Show();

                }
                catch (Exception ex)
                {
                    LBLloginStatus.Text = "Failed!";
                    MessageBox.Show(ex.Message + " " + Properties.Settings.Default.username + ":" + Properties.Settings.Default.password, "TwitterTray! - Login Failed");
                }

                this.Enabled = true;
                this.Cursor = Cursors.Default;
                this.Update();

            }

        }

        private void Login_Load(object sender, EventArgs e)
        {
            //Load USersettings if they are saved/wanted them to be saved...

            if (Properties.Settings.Default.savesettings)
            {
                txtPassword.Text = Properties.Settings.Default.password;
                txtUser.Text = Properties.Settings.Default.username;
                checkBox1.Checked = Properties.Settings.Default.savesettings;

                switch (Properties.Settings.Default.updatefreq)
                {
                    case 5:
                        comboBox1.SelectedIndex = 0;
                        break;

                    case 10:
                        comboBox1.SelectedIndex = 1;
                        break;

                    case 15:
                        comboBox1.SelectedIndex = 2;
                        break;

                    case 30:
                        comboBox1.SelectedIndex = 3;
                        break;

                    case 60:
                        comboBox1.SelectedIndex = 4;
                        break;


                }
            }
            
        }
    }
}
